package com.ymatou.iapi.sellerproduct.testcase;
import java.math.BigDecimal;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ActivityProductModifySortBean;
import com.ymatou.iapi.sellerproduct.service.ActivityProductModifySortCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ActivityProductModifySort {
	private static ActivityProductModifySortBean activityproductmodifysortBean;
	private static ActivityProductModifySortCall activityproductmodifysortCall;
	
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new  YmtProductsInLiveWapper();
	private static LiveProductInfoWapper liveProductInfoWapper = new LiveProductInfoWapper();
	private static LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
	private static LiveProductsWapper liveProductsWapper = new LiveProductsWapper();
	
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播待上架商品排序");
	}
	
	@Before
	public void caseUp() {
		activityproductmodifysortBean = new ActivityProductModifySortBean();
		activityproductmodifysortCall = new ActivityProductModifySortCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(activityproductmodifysortCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActivityProductModifySort_001() {
		Logger.start(true,"直播待上架商品排序");
		try{
			
			//1 创建直播
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 发布2个商品
			JSONObject productReqObject1 = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null);
			String productId1 =  productReqObject1.getJSONObject("Data").getString("ProductID");
			
			JSONObject productReqObject2 = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null);
			String productId2 =  productReqObject2.getJSONObject("Data").getString("ProductID");
			
			//3 add product to live
	    	SellerProductCallService.addProductInLive(sellerId,liveId,productId1);
	        SellerProductCallService.addProductInLive(sellerId,liveId,productId2);
	        
	        //SQL - Ymt_ProductsInLive
	        int Product1_sortId_SQL = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId1).get(0).get("dReadyPutawayProductSort").toString());
	        int Product2_sortId_SQL = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId2).get(0).get("dReadyPutawayProductSort").toString());
	        Logger.verifyEquals(true, (Product1_sortId_SQL - Product2_sortId_SQL)>0, "SQL-Ymt_ProductsInLive dReadyPutawayProductSort");
			
	        //Mongo
	        if (YmtMongoDriverFactroy.isOldMongoCheck()){
	        	//LiveProductInfo
	        	int Product1_sortId_Mgo = Integer.parseInt(liveProductInfoWapper.getliveproductByProductId(productId1).get("ReadyPutawayProductSort").toString());
	        	int Product2_sortId_Mgo = Integer.parseInt(liveProductInfoWapper.getliveproductByProductId(productId2).get("ReadyPutawayProductSort").toString());
	        	
	        	Logger.verifyEquals(Product1_sortId_SQL, Product1_sortId_Mgo, "Mongo-LiveProductInfo ReadyPutawayProductSort");
	        	Logger.verifyEquals(Product2_sortId_SQL, Product2_sortId_Mgo, "Mongo-LiveProductInfo ReadyPutawayProductSort");
	        }
	        
	        if (YmtMongoDriverFactroy.isNewMongoCheck()){
	        	//LiveProductModel
	        	int Product1_sortId_Mgo = Integer.parseInt(liveProductModelWapper.getByProductId(productId1).get("ReadyPutawayProductSort").toString());
	        	int Product2_sortId_Mgo = Integer.parseInt(liveProductModelWapper.getByProductId(productId2).get("ReadyPutawayProductSort").toString());
	        	
	        	Logger.verifyEquals(Product1_sortId_SQL, Product1_sortId_Mgo, "Mongo-LiveProductModel ReadyPutawayProductSort");
	        	Logger.verifyEquals(Product2_sortId_SQL, Product2_sortId_Mgo, "Mongo-LiveProductModel ReadyPutawayProductSort");
	        }
	        
	        if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
	        	//LiveProductModel
	        	//new BigDecimal(liveProductMap.get("sort").toString()).toPlainString()
	        	Map<String, Object> prod1SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductsWapper.getByProductId(productId1).get("sort").toString()).toPlainString());
	        	Map<String, Object> prod2SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductsWapper.getByProductId(productId2).get("sort").toString()).toPlainString());
	        	
	        	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product1_sortId_SQL), prod1SortMgo,"Mongo-LiveProducts sort");
	        	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product2_sortId_SQL), prod2SortMgo,"Mongo-LiveProducts sort");
	      
	        }
	       
			//排序
			activityproductmodifysortBean.setActivityId(liveId);
			activityproductmodifysortBean.setUserId(sellerId);
			activityproductmodifysortBean.setNewIndex(1);
			activityproductmodifysortBean.setOldIndex(2);
			activityproductmodifysortBean.setProductId(productId1);
			activityproductmodifysortCall.setData(activityproductmodifysortBean);
			activityproductmodifysortCall.callService();
			
			Logger.verifyEquals(200,new JSONObject(activityproductmodifysortCall.getReturnData()).getInt("Code"), "响应报文-返回码");

			
			   //SQL - Ymt_ProductsInLive
	        int Product1_sortId_SQL_After = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId1).get(0).get("dReadyPutawayProductSort").toString());
	        int Product2_sortId_SQL_After = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId2).get(0).get("dReadyPutawayProductSort").toString());
	        Logger.verifyEquals(true, (Product1_sortId_SQL_After - Product2_sortId_SQL_After)<0, "SQL-Ymt_ProductsInLive dReadyPutawayProductSort");
			
	        //Mongo
	        if (YmtMongoDriverFactroy.isOldMongoCheck()){
	        	//LiveProductInfo
	        	int Product1_sortId_Mgo = Integer.parseInt(liveProductInfoWapper.getliveproductByProductId(productId1).get("ReadyPutawayProductSort").toString());
	        	int Product2_sortId_Mgo = Integer.parseInt(liveProductInfoWapper.getliveproductByProductId(productId2).get("ReadyPutawayProductSort").toString());
	        	
	        	Logger.verifyEquals(Product1_sortId_SQL_After, Product1_sortId_Mgo, "Mongo-LiveProductInfo ReadyPutawayProductSort");
	        	Logger.verifyEquals(Product2_sortId_SQL_After, Product2_sortId_Mgo, "Mongo-LiveProductInfo ReadyPutawayProductSort");
	        }
	        
	        if (YmtMongoDriverFactroy.isNewMongoCheck()){
	        	//LiveProductModel
	        	int Product1_sortId_Mgo = Integer.parseInt(liveProductModelWapper.getByProductId(productId1).get("ReadyPutawayProductSort").toString());
	        	int Product2_sortId_Mgo = Integer.parseInt(liveProductModelWapper.getByProductId(productId2).get("ReadyPutawayProductSort").toString());
	        	
	        	Logger.verifyEquals(Product1_sortId_SQL_After, Product1_sortId_Mgo, "Mongo-LiveProductModel ReadyPutawayProductSort");
	        	Logger.verifyEquals(Product2_sortId_SQL_After, Product2_sortId_Mgo, "Mongo-LiveProductModel ReadyPutawayProductSort");
	        }
	        
	        
	        if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
	        	//LiveProductModel
	        	//new BigDecimal(liveProductMap.get("sort").toString()).toPlainString()
	        	Map<String, Object> prod1SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductsWapper.getByProductId(productId1).get("sort").toString()).toPlainString());
	        	Map<String, Object> prod2SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductsWapper.getByProductId(productId2).get("sort").toString()).toPlainString());
	        	
	        	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product1_sortId_SQL_After), prod1SortMgo,"Mongo-LiveProducts sort1");
	        	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product2_sortId_SQL_After), prod2SortMgo,"Mongo-LiveProducts sort");
	      
	        }
	        
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
		
	}
}
